/**
 * 解析bsp数据生成路由path列表
 * @param menusAll
 * @returns {Array}
 */
// const menus_to_urlPath = (menusAll = []) => {
//   let list = JSON.parse(JSON.stringify(menusAll));
//
//   let authUrlList = [];
//
//   function setAuthList(menus, father) {
//     menus.forEach(element => {
//       let url = "";
//       // 如果有父级则把父级放入前面
//       if (father) {
//         url = father + "/" + element.key;
//       } else {
//         url = element.key;
//       }
//       authUrlList.push(url);
//       if (element.subs) {
//         setAuthList(element.subs, url);
//       }
//     });
//   }
//
//   setAuthList(list, null);
//
//   return authUrlList;
// };

/**
 * 解析bsp数据生成菜单栏数据
 * @param menusAll
 * @param menuIcons [ { label: '项目推进', icon: 'el-icon-s-order' }, { label: '系统管理', icon: 'el-icon-s-tools' }, { label: '系统管理', icon: 'icon-Set-up' } ]
 * @returns {any}
 */
 export const setMenuList = (menusAll = [], menuIcons = []) => {
    const list = JSON.parse(JSON.stringify(menusAll))

    function setMenu(menuCopy, index) {
        menuCopy.forEach(item => {
            // 如果是第一次则匹配图标
            const findIcon = menuIcons.find(findItem => {
                return findItem.label === item.label
            })
            if (findIcon) {
                item.icon = findIcon.icon
            }
            if (item.subs && item.subs.length === 0) {
                item.subs = null
            } else if (item.subs && item.subs.length > 0) {
                setMenu(item.subs, index++)
            }
            //  this.menus.push(item,index++)
        })
    }

    setMenu(list, 0)

    return list
}

/**
 * bsp返回数据格式解析生成路由
 * @param menusAll bsp返回菜单数据
 * @param routesAll 本地全部路由
 * @param whiteListMenu 本地白名单路由
 * @returns {*[]} //根据菜单权限匹配的路由集合
 */
export const formateRouteConfig = (menusAll = [], routesAll = [], whiteListMenu = []) => {
    // console.log(menus_to_urlPath(menusAll))
    // 解析bsp-systemMenus格式生成路由配置
    const routesAllDic = {}
    routesAll.forEach(item => {
        routesAllDic[item.path] = item
    })
    const list = []

    function deCode(d) {
        d.forEach(menu => {
            if (menu.subs && menu.subs.length > 0) {
                deCode(menu.subs)
            } else {
                const m = menu.code.split('::')
                m.splice(0, 1)
                const mStr = '/' + m.join('/')
                try {
                    if (routesAllDic[mStr].path === mStr) {
                        list.push(routesAllDic[mStr])
                    }
                } catch (e) {
                    console.log(e)
                }
            }
        })
    }

    deCode(menusAll)

    const routeAll = [...list, ...whiteListMenu]

    // 通配符必须最后添加-否则报错
    routeAll.push({
        path: '/*',
        name: '*',
        meta: {},
        component: () => import('@/views/UnderConstruction'),
    })

    return {
        routeAll,
        bspRoute: list,
    }
}
